package cn.seqdata.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.Auditable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties({ "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate" })
public abstract class JpaAuditable<ID extends Serializable> implements Auditable<Long, ID> {
	private static final long serialVersionUID = 1L;

	private Long createdBy;
	private DateTime createdDate;
	private Long lastModifiedBy;
	private DateTime lastModifiedDate;

	@JsonIgnore
	@Transient
	@Override
	public boolean isNew() {
		return null == getId();
	}

	@Override
	@CreatedBy
	@Column(name = "created_by", updatable = false)
	public Long getCreatedBy() {
		return this.createdBy;
	}

	@Override
	public void setCreatedBy(Long createdBy) {
		this.createdBy = createdBy;
	}

	@Override
	@CreatedDate
	@Column(name = "created_date", updatable = false)
	@Convert(converter = cn.seqdata.jpa.converter.DateTimeConverter.class)
	public DateTime getCreatedDate() {
		return this.createdDate;
	}

	@Override
	public void setCreatedDate(DateTime createdDate) {
		this.createdDate = createdDate;
	}

	@Override
	@LastModifiedBy
	@Column(name = "last_modified_by", insertable = false)
	public Long getLastModifiedBy() {
		return this.lastModifiedBy;
	}

	@Override
	public void setLastModifiedBy(Long lastModifiedBy) {
		this.lastModifiedBy = lastModifiedBy;
	}

	@Override
	@LastModifiedDate
	@Column(name = "last_modified_date", insertable = false)
	@Convert(converter = cn.seqdata.jpa.converter.DateTimeConverter.class)
	public DateTime getLastModifiedDate() {
		return this.lastModifiedDate;
	}

	@Override
	public void setLastModifiedDate(DateTime lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}
}
